package com.xmkj.bearbiz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.entity.carbean.OrderDetailBean;
import com.common.retrofit.methods.CancleOrderMethods;
import com.common.retrofit.methods.GetOrderFrameInfoMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.EmptyUtils;
import com.xmkj.bearbiz.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/13.
 */

public class OrderDetalRecordActivitiy extends BaseMvpActivity {
    public static final String ORDERID = "orderid";
    @BindView(R.id.tv_service)
    TextView tvService;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_name_again)
    TextView tvNameAgain;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.et_mobile)
    TextView etMobile;
    @BindView(R.id.tv_create_time)
    TextView tvCreateTime;
    @BindView(R.id.tv_beizhu)
    TextView tvBeizhu;
    @BindView(R.id.tv_stasus)
    TextView tvStatus;
    private String orderid;
    private OrderDetailBean infoBean;
    private int status;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_detail;
    }

    @Override
    protected void onViewCreated() {
//        Intent intent = getIntent();
//        if (null != intent) {
//            Bundle bundle = getIntent().getExtras();
//            if(bundle!=null){
//                orderid = bundle.getString(ORDERID);
//                if (EmptyUtils.isNotEmpty(orderid)) {
//                    gotoGetOrderInfo();
//                }
//            }
//
//        }
    }

    @Override
    protected void doLogicFunc() {

        Intent intent = getIntent();
        if (null != intent) {
            Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                orderid = bundle.getString(ORDERID);
                if (EmptyUtils.isNotEmpty(orderid)) {
                    gotoGetOrderInfo();
                }
            }

        }


        orderid = intent.getStringExtra(ORDERID);
        if (EmptyUtils.isNotEmpty(orderid)) {
            gotoGetOrderInfo();
        }
        attachClickListener(tvStatus);

    }

    @Override
    protected void onViewClicked(View view) {
        if (view.getId() == tvStatus.getId()) {
            if (infoBean.status == 1) {
                //取消订单
                gotoCancleOrder();
            } else if (infoBean.status == 5) { //去评价
                Intent intent = new Intent(context, PingJiaActivity.class);
                intent.putExtra(PingJiaActivity.ORDERID, infoBean.id + "");
                startActivity(intent);
            } else if (infoBean.status == 4) {  // 去支付
                Intent intent = new Intent(context, PayActivity.class);
                intent.putExtra(PayActivity.TOTAL, infoBean.total);
                intent.putExtra(PayActivity.ORDERID, infoBean.id + "");
                startActivity(intent);
            }
        }
    }

    private void gotoCancleOrder() {
        showProgressingDialog();
        CommonSubscriber<Object> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                onBackPressed();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });
        CancleOrderMethods.getInstance().cancelOrder(subscriber, uid, hashid, infoBean.id);
        rxManager.add(subscriber);
    }

    @Override
    protected void setNavigation() {
        setNavigationBack("订单详情");
    }

    private void gotoGetOrderInfo() {
        CommonSubscriber<OrderDetailBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                infoBean = (OrderDetailBean) o;
                dismissProgressDialog();
                tvAddress.setText(infoBean.address);
                tvNameAgain.setText(infoBean.nickname);
                if (EmptyUtils.isNotEmpty(infoBean.sub_time)) {
                    tvTime.setText(infoBean.sub_time);
                } else {

                    tvTime.setText("现在");
//                    Date d = new Date();
//                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    System.out.println("当前时间：" + sdf.format(d));
//                    tvTime.setText(sdf.format(d));
                }
                etMobile.setText(infoBean.mobile);
                tvCreateTime.setText(infoBean.create_time);
                tvService.setText("洗车");
                if (EmptyUtils.isNotEmpty(infoBean.say)) {
                    tvBeizhu.setText(infoBean.say);
                } else {
                    tvBeizhu.setText("暂无备注");
                }
                tvMoney.setText(infoBean.total);
                status = infoBean.status;
                switch (infoBean.status) {

                    case 1:
                        tvStatus.setText("取消订单");
                        tvStatus.setEnabled(true);  // 接单
                        break;
                    case 2:
                        tvStatus.setText("在路上");
                        tvStatus.setEnabled(false);// 接单
                        break;
                    case 3:
                        tvStatus.setText("洗车中");  // 确认到达
                        tvStatus.setEnabled(false);
                        break;
                    case 4:
                        tvStatus.setText("去支付");
                        tvStatus.setEnabled(true);
                        break;
                    case 5:
                        tvStatus.setText("去评价");  // 确认到达
                        tvStatus.setEnabled(true);
                        break;
                    case 6:
                        tvStatus.setText("已评价");  // 确认到达
                        tvStatus.setEnabled(false);
                        break;
                    case 7:
                        tvStatus.setText("已取消");  // 确认到达
                        tvStatus.setEnabled(false);
                        break;
//                    case 4:
//                        tvStatus.setText("洗车完成");  // 确认完成
//                        tvStatus.setEnabled(true);
//                        break;
//                    case 5:
//                        tvStatus.setText("待评价");
//                        tvStatus.setEnabled(false);
//                        break;
//                    case 6:
//                        tvStatus.setText("已评价");
//                        tvStatus.setEnabled(false);
//                        break;
//                    case 7:
//                        tvStatus.setText("已取消");
//                        tvStatus.setEnabled(false);
//                        break;

                }

            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

        GetOrderFrameInfoMethods.getInstance().orderDetail(subscriber, uid, hashid, orderid);
        rxManager.add(subscriber);
    }

    @Override
    public void bindKnife() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        gotoGetOrderInfo();
    }
}
